public class MaxSubarraySum {
    public static int findMaxSubarraySum(int[] nums) {
        int maxSum = nums[0]; // 初始化最大和为第一个元素
        int currentSum = nums[0]; // 初始化当前和为第一个元素

        for (int i = 1; i < nums.length; i++) {
            // 判断当前和加上当前元素与当前元素哪个大，并更新当前和
            currentSum = Math.max(currentSum + nums[i], nums[i]);

            // 更新最大和
            maxSum = Math.max(maxSum, currentSum);
        }

        return maxSum;
    }

    public static void main(String[] args) {
        int[] nums = {1, -2, 3, 5, -1};
        int maxSubarraySum = findMaxSubarraySum(nums);
        System.out.println("The maximum subarray sum is: " + maxSubarraySum);
    }
}
